package cn.tedu.jdbc;

import java.sql.*;

public class JDBCUtils {
    public static Connection get()throws Exception {
        //1，注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2，获取连接
        String url = "jdbc:mysql://localhost:3307/cgb211101?characterEncoding=utf8";
        Connection c = DriverManager.getConnection(url, "root", "root");
         return c;
    }

    static public void close (ResultSet r, PreparedStatement p , Connection c ){
        //4,释放资源
        if (p !=null){//防止报出nullpointerExcrption的异常
            try {
                p.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                p=null;
            }
        }

        if (c != null){
            try {
                c.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                c=null;
            }
        }

        if (r!= null){
            try {
                r.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                r=null;
            }
        }
    }
}
